<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :search.sync="search"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.joblog_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot-scope="scope" slot="agetriggerTimeSearch">
        <el-input placeholder="自定义输入框" :disabled="disabled" :size="size" style="width:200px" v-model="search.age"></el-input>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, trigger, getRegister, getNextExecTime, taskStart, taskStop} from "@/api/job/log";
  import {mapGetters} from "vuex";
  import {baseUrl} from "../../config/env";

  export default {
    data() {
      return {
        form: {},
        search: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          searchShow: true,
          searchMenuSpan: 6,
          addBtn: false,
          dateBtn: true,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "执行器ID",
              prop: "jobGroup",
              search: true,
              // type: "select",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }],
              searchRules: [{
                required: true,
                message: "请输入执行器ID",
                trigger: "blur"
              }],
            },
            {
              label: "任务ID",
              prop: "jobId",
              search: true,
              // type: "select",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "调度时间",
              prop: "triggerTime",
              type: "datetime",
              search: false,
              // searchRange: true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "执行地址",
              prop: "executorAddress",
              search: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "执行时间",
              prop: "handleTime",
              search: false,
              type: "datetime",
              searchRange: true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "执行结果",
              prop: "handleCode",
              search: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
          ]
        },
        data: [],
      };
    },
    watch: {
      expression(old, newval) {}
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.joblog_add, false),
          viewBtn: this.vaildData(this.permission.joblog_view, false),
          delBtn: this.vaildData(this.permission.joblog_delete, false),
          editBtn: this.vaildData(this.permission.joblog_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },

      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      beforeHandleCommand(type, row) {
        return {
          type,
          row
        }
      },
      handleCommand(command) {
        let type = command.type
        let scope = command.row
        let that = this;
        switch (type) {
          case "trigger":
            trigger(scope.row.id, scope.row.executorParam, scope.row.jobGroup);
            break;
          case "querylog":
            break;
          case "register":
            that.openResister = true
            that.registryListData = []
            // registryListData
            getRegister(scope.row.id).then(res => {
              let registryList = res.data.data.registryList;
              for (let i = 0; i < registryList.length; i++) {
                that.registryListData.push({"index": i, "address": registryList[i]})
              }
            });
            break;
          case "nextexectime":
            that.openNextExecTime = true
            that.nextExecTimeListData = []
            //nextTime

            getNextExecTime(scope.row.scheduleType, scope.row.scheduleConf).then(res => {
              let nextTimeList = res.data.data;
              for (let i = 0; i < nextTimeList.length; i++) {
                that.nextExecTimeListData.push({"nextTime": nextTimeList[i]})
              }

            })
            break;
          case "start":
            taskStart(scope.row.id);
            break;
          case "stop":
            taskStop(scope.row.id);
            break
          default:
            break
        }

      },
      // triggerOnce(scope) {
      //   console.log("triggerOnce")
      //   console.log(scope)
      //
      //   trigger(scope.row.id, scope.row.executorParam, scope.row.jobGroup).then(res => {
      //     console.log(res);
      //   })
      // },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data;
          this.page.total = data.count;
          this.data = data.data;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
